#include "stdafx.h"
#include "VolCtrl.h"

namespace SOUI
{
	SVolCtrl::SVolCtrl()// :m_bFlag(false)
	{
	}


	SVolCtrl::~SVolCtrl()
	{
	}


	CRect SVolCtrl::GetCheckRect()
	{
		CRect rcClient;
		GetClientRect(rcClient);
		SASSERT(m_pBgSkin);
		CSize szCheck = m_pBgSkin->GetSkinSize();
		CRect rcCheckBox(rcClient.TopLeft(), szCheck);
		rcCheckBox.OffsetRect(0, (rcClient.Height() - szCheck.cy) / 2);
		return rcCheckBox;
	}

	void SVolCtrl::OnPaint(IRenderTarget *pRT)
	{
		CRect rcCheckBox = GetCheckRect();
		m_pBgSkin->Draw(pRT, rcCheckBox, _GetDrawState());
	}
	//
#define WndState_Loud 0x00000020UL
	UINT SVolCtrl::_GetDrawState()
	{
		DWORD dwState = GetState();
		UINT uState = 0;

		if (m_pBgSkin)
		{
			if (dwState & WndState_Check)
			{
				uState = 2;
			}
			else if (dwState  & WndState_Loud)
			{
				uState = 1;
			}

		}
		return uState;
	}
	void SVolCtrl::SetLoud(bool isLoud)
	{
		if (isLoud)
			ModifyState(WndState_Loud, 0, TRUE);
		else
			ModifyState(0, WndState_Loud, TRUE);
		Invalidate();
	}
	void SVolCtrl::SetMuted(bool bMuted)
	{
		if (bMuted&&!IsChecked())
		{
			ModifyState(WndState_Check,0, TRUE);
			Invalidate();
		}
		else if (IsChecked())
		{
			ModifyState(0,WndState_Check, TRUE);
			Invalidate();
		}
	}
	CSize SVolCtrl::GetDesiredSize(LPCRECT pRcContainer)
	{
		SASSERT(m_pBgSkin);
		CSize szCheck = m_pBgSkin->GetSkinSize();
		CSize szRet = __super::GetDesiredSize(pRcContainer);
		szRet.cx += szCheck.cx;
		szRet.cy = max(szRet.cy, szCheck.cy);
		return szRet;
	}
	void SVolCtrl::OnLButtonUp(UINT nFlags, CPoint point)
	{
		__super::OnLButtonUp(nFlags, point);
		if (IsChecked())
			ModifyState(0, WndState_Check, TRUE);
		else
			ModifyState(WndState_Check, 0, TRUE);
		Invalidate();
	}

	void SVolCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
	{
		if (nChar == VK_SPACE)
		{
			if (IsChecked())
				ModifyState(0, WndState_Check, TRUE);
			else
				ModifyState(WndState_Check, 0, TRUE);
			Invalidate();
			FireCommand();
		}
	}
}